<?php
class controller_reader extends controller_base {
	
	public function action_index() {
		
	}
	
	public function action_list() {
		$model_reader = new model_reader();
		
		$config = $model_reader->getConfig();
		
		$result = array();
		foreach ($config as $key => $item) {
			$result[$key]['config'] = $item;
			
			$url = $item['url'];
			$pattern = $item['pattern'];
			$expire = isset($item['expire']) ? rand($item['expire'] / 2, $item['expire']) : rand(3600, 7200);
			$keys = isset($item['keys']) ? $item['keys'] : array('url', 'title');
			$offset = isset($item['offset']) ? $item['offset'] : 0;
			$limit = isset($item['limit']) ? $item['limit'] : 200;
			$charset = isset($item['charset']) ? $item['charset'] : '';
			
			$result[$key]['data'] = $model_reader->get($url, $pattern, $expire, $keys, $offset, $limit, $charset);
		}
		
		$this->assign('result', $result);
	}
	
	public function action_view() {
		$model_reader = new model_reader();

		$config = $model_reader->getConfig();

		$patterns = array(
			'content' => $config[$_GET['site']]['content']['content'],
			'title' => $config[$_GET['site']]['content']['title'],
		);
		$charset = isset($config[$_GET['site']]['content']['charset']) ? $config[$_GET['site']]['content']['charset'] : '';
		$url = base64_decode($_GET['url']);

		$result = $model_reader->get_item($url, $patterns, $charset);

		$this->assign('result', $result);
	}
	
}
